package org.ohos.rajawali3d.examples.examples.scene;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ProgressBar;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.opengl.Color;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.animation.Animation;
import org.rajawali3d.animation.RotateOnAxisAnimation;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.methods.DiffuseMethod;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Cube;
import org.rajawali3d.renderer.ISurfaceRenderer;

import java.util.Random;

/**
 * @author Jared Woolston (jwoolston@keywcorp.com)
 */
public class ObjectAddRemoveFragment extends AExampleFragment {

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        // Inflate the view
        Component component = inflater.parse(ResourceTable.Layout_rajawali_object_add_remove_fragment,
                container, true);

        container.moveChildToFront(component);
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);

        component.findComponentById(ResourceTable.Id_add_cube).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                ((ObjectAddRemoveRenderer) mRenderer).addNewCube();
            }
        });
        component.findComponentById(ResourceTable.Id_remove_cube).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                ((ObjectAddRemoveRenderer) mRenderer).removeRandomCube();
            }
        });
        // Create the loader
        mProgressBarLoader = (ProgressBar) mLayout.findComponentById(ResourceTable.Id_progress_bar_loader);
        mProgressBarLoader.setVisibility(Component.HIDE);

        // Create the renderer
//        mRenderer = createRenderer();
//        onBeforeApplyRenderer();
//        applyRenderer();
        return mLayout;
    }

    @Override
    public ISurfaceRenderer createRenderer() {
        return new ObjectAddRemoveRenderer(getContext(), this);
    }

    public static final class ObjectAddRemoveRenderer extends AExampleRenderer {

        private final Random random = new Random();
        private Material mCubeMaterial;

        ObjectAddRemoveRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            DirectionalLight light = new DirectionalLight();
            light.setPower(1);
            light.setLookAt(0, 0, -1);
            light.enableLookAt();
            getCurrentScene().setBackgroundColor(Color.BLACK);
            getCurrentScene().addLight(light);

            //
            // -- Create a material for all cubes
            //

            mCubeMaterial = new Material();
            mCubeMaterial.enableLighting(true);
            mCubeMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());

            getCurrentCamera().setZ(10);

            addNewCube();
        }

        public void addNewCube() {
            Cube cube = new Cube(1);
            cube.setPosition(-5 + random.nextFloat() * 10, -5 + random.nextFloat() * 10, random.nextFloat() * -10);
            cube.setMaterial(mCubeMaterial);
            cube.setColor(0x666666 + random.nextInt(0x999999));
            getCurrentScene().addChild(cube);

            Vector3 randomAxis = new Vector3(random.nextFloat(), random.nextFloat(), random.nextFloat());
            randomAxis.normalize();

            RotateOnAxisAnimation anim = new RotateOnAxisAnimation(randomAxis, 360);
            anim.setTransformable3D(cube);
            anim.setDurationMilliseconds(3000 + (int) (random.nextDouble() * 5000));
            anim.setRepeatMode(Animation.RepeatMode.INFINITE);
            getCurrentScene().registerAnimation(anim);
            anim.play();
        }

        public void removeRandomCube() {
            final int count = getCurrentScene().getNumChildren();
            if (count > 0) {
                final int index = random.nextInt(count);
                final Object3D child = getCurrentScene().getChildrenCopy().get(index);
                getCurrentScene().removeChild(child);
                child.destroy();
            }
        }
    }
}
